package co.edu.eam.ingsoft.jpa.reflect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DataBaseProcessor {

	private Connection connection;

	public DataBaseProcessor(Class<?>... clazzs) {
		try {
			System.out.println("Conectando");
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:XE", "JPA", "JPA");
			System.out.println("Conecto");
			createTables(clazzs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void createTables(Class<?>... clazzs) {
		EntityProcessor entityProcessor = new EntityProcessor();
		for (Class<?> clazz : clazzs) {
			String tableName = entityProcessor.getTableName(clazz);
			String fieldId = entityProcessor.getFieldId(clazz);
			ArrayList<String> columns = entityProcessor.getColumnsName(clazz);
			createTable(tableName, fieldId, columns);
		}
	}

	public void createTable(String tableName, String fieldId,
			ArrayList<String> columns) {
		String column = ",";
		for (String string : columns) {
			column += string + ",";
		}
		String[] fiel = fieldId.split(" ");
		String createTable = "CREATE table " + tableName + " (" + fieldId
				+ column + "constraint " + tableName + "_PK primary key ("
				+ fiel[0] + "))";
		try {
			PreparedStatement statement = connection
					.prepareStatement(createTable);
			statement.execute();
			System.out.println(createTable);
		} catch (SQLException e) {
			System.out.println("La tabla ya existe");
			// e.printStackTrace();
		}
	}

	public void insert(String tableName, String fieldId,
			ArrayList<String> columns, String idValue, ArrayList<String> values) {
		String column = ",";
		for (String string : columns) {
			column += string.split(" ")[0] + ",";
		}
		String value = ",";
		for (String string : values) {
			value += "'" + string + "',";
		}
		String insert = "INSERT INTO " + tableName + " ("
				+ fieldId.split(" ")[0]
				+ column.substring(0, column.length() - 1) + ") " + "VALUES ('"
				+ idValue + "'" + value.substring(0, value.length() - 1) + ")";
		try {
			PreparedStatement statement;
			statement = connection.prepareStatement(insert);
			statement.executeQuery(insert);
			System.out.println(insert);

		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("No se pudo insertar:" + insert);
		}
	}

	public void update(String tableName, String fieldId,
			ArrayList<String> columns, String idValue, ArrayList<String> values) {
		String column = "";
		for (int i = 0; i < columns.size(); i++) {
			String string = columns.get(i);
			column += string.split(" ")[0] + "='" + values.get(i) + "',";
		}
		String update = "UPDATE " + tableName + " SET "
				+ column.substring(0, column.length() - 1) + " WHERE "
				+ fieldId.split(" ")[0] + "='" + idValue + "'";
		try {
			PreparedStatement statement;
			statement = connection.prepareStatement(update);
			statement.executeQuery(update);
			System.out.println(update);

		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("No se pudo actualizar:" + update);
		}
	}

	public void delete(String tableName, String fieldId, String idValue) {
		String delete = "DELETE FROM " + tableName + " WHERE "
				+ fieldId.split(" ")[0] + "='" + idValue + "'";
		try {
			PreparedStatement statement;
			statement = connection.prepareStatement(delete);
			statement.executeQuery(delete);
			System.out.println(delete);

		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("No se pudo eliminar:" + delete);
		}
	}

	public Object select(Object t, String tableName, String fieldId,
			ArrayList<String> columns, String idValue, ArrayList<String> values) {
		try {
			String column = ",";
			for (String string : columns) {
				column += string.split(" ")[0] + ",";
			}
			String select = "SELECT " + fieldId.split(" ")[0]
					+ column.substring(0, column.length() - 1) + " FROM "
					+ tableName + " WHERE " + fieldId.split(" ")[0] + "='"
					+ idValue + "'";
			PreparedStatement statement;
			System.out.println(select);
			statement = connection.prepareStatement(select);
			ResultSet res = statement.executeQuery(select);

			if (res.next()) {
				for (String string : columns) {
					column = string.split(" ")[0] + ",";
					res.getString(column);
				}
				String nombre = res.getString(1);
				String cedula = res.getString(2);
				String telefono = res.getString(3);
				String direccion = res.getString(4);
				String correo = res.getString(5);

			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return t;
	}
}
